package com.gitee.wsl.data.input.viewmodel.scopes


import com.gitee.wsl.coroutines.Queued
import com.gitee.wsl.data.input.SideJobScope
import com.gitee.wsl.data.input.interceptor.BallastInterceptor
import com.gitee.wsl.data.input.viewmodel.actors.EventActor
import com.gitee.wsl.data.input.viewmodel.actors.InputActor
import com.gitee.wsl.data.input.viewmodel.actors.InterceptorActor
import kotlinx.coroutines.CoroutineScope

internal class SideJobScopeImpl<Inputs : Any, Events : Any, State : Any>(
    sideJobCoroutineScope: CoroutineScope,

    private val inputActor: InputActor<Inputs, Events, State>,
    private val eventActor: EventActor<Inputs, Events, State>,
    private val interceptorActor: InterceptorActor<Inputs, Events, State>,

    override val key: String,
    override val restartState: SideJobScope.RestartState,
) : SideJobScope<Inputs, Events, State>, CoroutineScope by sideJobCoroutineScope {

    override suspend fun postInput(input: Inputs) {
        inputActor.enqueueQueued(Queued.HandleInput(null, input), await = false)
    }

    override suspend fun postEvent(event: Events) {
        eventActor.enqueueEvent(event, null, false)
    }

    override suspend fun <I : BallastInterceptor<*, *, *>> getInterceptor(key: BallastInterceptor.Key<I>): I {
        return interceptorActor.getInterceptor(key)
    }
}
